package edu.hawaii.halealohacli.jcev.command;

import java.util.Map;
import org.wattdepot.client.WattDepotClient;

/**
 * The help command which prints the descriptions of all valid functions.
 * @author Christopher Foo
 *
 */
public class Help implements Command {

  /**
   * Map containing all valid commands where the user invocation is the key and the value is
   * the corresponding command's object.
   */
  private Map<String, Command> commands;

  /**
   * Creates a new Help object with the given Map.
   * @param commands The Map containing all valid commands.
   */
  public Help(Map<String, Command> commands) {
    this.commands = commands;
  }

  @Override
  /**
   * Verifies the arguments and prints the descriptions of all valid commands if invoked correctly.
   * @param args Space delimited string of arguments (i.e. "arg1 arg2 arg3").
   * @param client The WattDepot client that will be performing the queries.
   * @throws Exception If an exception was thrown by the command (i.e. bad arguments)
   * or any of its function calls.
   */
  public void execute(String args, WattDepotClient client) throws Exception {
    if (verifyArguments(args)) {
      
      System.out.println("Here are the available commands for this system.");
      
      for (Command command : commands.values()) {
        if (command != null) {
          System.out.println(command.getHelpString());
        }
      }
      
      System.out.println("Note: towers are:  Mokihana, Ilima, Lehua, Lokelani\n"
          + "Lounges are the tower names followed by a \"-\" followed by one of"
          + " A, B, C, D, E. For example, Mokihana-A.");
      
    }
    else {
      throw new Exception("Bad input: More than 0 arguments." + " Correct Syntax: help");
    }
  }

  @Override
  /**
   * Returns the string to be printed by the help command.
   * Format:
   * "command-name [args1] [args2] .... \nDescription"
   * @return The string to be printed by the help command.
   */
  public String getHelpString() {
    return "help\n" + "Lists descriptions of all valid commands.";
  }

  /**
   * Checks that the arguments are valid.
   * 
   * @param args The string of arguments.
   * @return If the arguments are valid.
   */
  public boolean verifyArguments(String args) {
    return "".equals(args.trim());
  }
}
